﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Description résumée de CompagnieManager
/// </summary>
/// 
namespace DAL
{
    public class CompagnieManager : ICompagnieManager
    {
        private IAATOEntities dbService;

        public CompagnieManager()
        {
            dbService = new IAATOEntities();
        }

        public bool addCompagnie(COMPAGNIE compagnie)
        {
            try
            {
                dbService.COMPAGNIEJeu.AddObject(compagnie);
                dbService.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool deleteCompagnie(int compagnieId)
        {
            try
            {
                COMPAGNIE compagnie = (from c in dbService.COMPAGNIEJeu
                                 where c.ID == compagnieId
                                 select c).First();

                dbService.DeleteObject(compagnie);
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }

        }

        public List<COMPAGNIE> getAllCompagnies()
        {
            try
            {
                return (from c in dbService.COMPAGNIEJeu
                        orderby c.NOM
                        select c).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool updateCompagnie()
        {
            try
            {
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public COMPAGNIE getCompagnieById(int id)
        {
            try
            {
                return (from c in dbService.COMPAGNIEJeu
                        where c.ID == id
                        select c).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string getNom(int id)
        {
            try
            {
                return (from c in dbService.COMPAGNIEJeu
                        where c.ID == id
                        select c.NOM).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public COMPAGNIE getCompagnieByNom(string nom)
        {
            try
            {
                return (from c in dbService.COMPAGNIEJeu
                        where c.NOM == nom
                        select c).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}